home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / music / midiplay.lzh / MP_MAIN.C < prev   
C/C++ Source or Header  |  1991-11-30  |  5KB  |  220 lines

  1. /*
  2.  * File: mp_main.c
  3.  * SGoldthorpe    20-Jul-91
  4.  */
  5.  
  6. /*
  7.  * mp_main - main bit of midiplay
  8.  * This software is (C) 1991 Stephen Goldthorpe but it's FREE!  Usual
  9.  * disclaimers and notices about this software not being sold for profit.
  10.  * But you may take all you want from the code though!  If you have any
  11.  * suggestions/bug fixes please get in contact with me.  I don't want to
  12.  * maintain code i've never even seen before (life's hard enough without all
  13.  * of that)!
  14.  *                        -Steve Goldthorpe
  15.  * Phone (DAYTIME UK):    +44 707 382350
  16.  * Internet E-Mail:    SGoldthorpe.wgc-e@rx.xerox.com
  17.  *            goldthor@arisia.xerox.com
  18.  *
  19.  * Version 0.5 by Piet van Oostrum <piet@cs.ruu.nl>
  20.  * November 1991.
  21.  * Added -T option
  22.  * Support for files > 32KB
  23.  * See also mp_intp.c
  24.  */
  25.  
  26. #include    <stdio.h>
  27. #include    <types.h>
  28. #include    <stat.h>
  29. #include    <fcntl.h>
  30. #include    <malloc.h>
  31. #include    <errno.h>
  32.  
  33. /* #include    "midiplay.h" included in mp_gbls.h */
  34. #include    "mp_gbls.h"
  35.  
  36. /* EXTERNAL DECLS */
  37. extern int    interp();
  38. extern long lread();
  39. extern char * lalloc();
  40.  
  41. /* GLOBAL VARIABLES */
  42. BYTE    *file_buff;
  43. long    buf_size;
  44.  
  45. /* FUNCTION DECLS */
  46. static BOOL    play();
  47. static void    parse_flags(), do_help();
  48.  
  49. /* MAIN CODE - SHORT & SWEET */
  50. int    main(argc,argv)
  51. int    argc;
  52. char    *argv[];
  53. {   int    arg_n;
  54.  
  55.     /* title (so you and I know what's what) */
  56.     printf("%s v %d.%d (%s)\n",app_name,RELEASE,VERSION,DATE);
  57.  
  58.     /* check args */
  59.     if(argc<2)
  60.     {    fprintf(stderr,"%s: -h | {-CPSTcilmnpt} <file1> {...<fileN>}\n",
  61.         app_name);
  62.     exit();
  63.     };
  64.  
  65.     /* get flags from arg list */
  66.     for(arg_n=1;arg_n<argc;arg_n++)
  67.     if(*argv[arg_n]=='-')
  68.         parse_flags(argv[arg_n]);
  69.  
  70.     /* now get some workspace... */
  71. #ifdef DEBUG
  72.     fprintf(stderr,"%s: malloc (%d)\n",app_name,BUFFER_SIZE);
  73. #endif
  74.     if((file_buff=(BYTE*)malloc(BUFFER_SIZE))==NULL)
  75.     {    fprintf(stderr,"%s: couldn't get enough memory for buffer\n",app_name);
  76.     exit();
  77.     };
  78.     buf_size = BUFFER_SIZE;
  79.  
  80.     /* play all files in arg list */
  81.     for(arg_n=1;arg_n<argc;arg_n++)
  82.     if(*argv[arg_n]!='-')
  83.         if(play(argv[arg_n]))
  84.         break;
  85.  
  86.     /* and tidy up */
  87.     free((char*)file_buff);
  88. };
  89.  
  90. /* FUNCTION DEFS */
  91. static void    parse_flags(flags)
  92. char    *flags;
  93. {   int    c;
  94.     for(c=1;c<strlen(flags);c++)
  95.     switch(*(flags+c))
  96.     {   case 'h':
  97.         do_help();
  98.         exit();
  99.         case 't':
  100.         f_text=TRUE;
  101.         break;
  102.         case 'c':
  103.         f_copyright=TRUE;
  104.         break;
  105.         case 'n':
  106.         f_track_name=TRUE;
  107.         break;
  108.         case 'i':
  109.         f_instrument=TRUE;
  110.         break;
  111.         case 'l':
  112.         f_lyric=TRUE;
  113.         break;
  114.         case 'm':
  115.         f_marker=TRUE;
  116.         break;
  117.         case 'p':
  118.         f_prompt=TRUE;
  119.         break;
  120.         case 'P':
  121.         f_Program=TRUE;
  122.         break;
  123.         case 'C':
  124.         f_Channel_pressure=TRUE;
  125.         break;
  126.         case 'S':
  127.         f_Sysex = TRUE;
  128.         break;
  129.         case 'T':
  130.         f_Timing = TRUE;
  131.         break;
  132.         default:
  133.         fprintf(stderr,"bad flag '%c'\n",*(flags+c));
  134.         break;
  135.         };
  136. };
  137.  
  138. static void    do_help()
  139. {    printf("help\n");
  140.     printf("    %s plays midi files format 0 and 1.\n",app_name);
  141.     printf("syntax:\n");
  142.     printf("    %s -h | {-CPScilmnpt} <file1> {...<fileN>}\n",app_name);
  143.     printf("where\n");
  144.     printf("    h = This help message,\n");
  145.     printf("    C = Send channel pressure data,\n");
  146.     printf("    P = Send program data,\n");
  147.     printf("    S = Send system exclusive data,\n");
  148.     printf("    T = Send timing data,\n");
  149.     printf("    c = Print copyright messages,\n");
  150.     printf("    i = Print instrument assignments,\n");
  151.     printf("    m = Print song markers,\n");
  152.     printf("    n = Print sequence, track names,\n");
  153.     printf("    p = Print cueing prompts,\n");
  154.     printf("    t = Print general text information.\n\n");
  155.     printf("%s can be stopped by CTRL C and tracks skipped with CTRL S.\
  156. \n",app_name);
  157. };
  158.  
  159. static BOOL    play(file)
  160. char    *file;
  161. {   struct stat    stat_buff;
  162.     long    len;
  163.     int        fd;
  164. #ifdef DEBUG
  165.     fprintf(stderr,"%s: play %s\n",app_name,file);
  166. #endif
  167.  
  168.     /* open the file (is possible)  and get info */
  169.     if((fd=open(file,O_RDONLY))<0)
  170.     {    fprintf(stderr,"%s: can't open %s\n",app_name,file);
  171.     return(FALSE);
  172.     };
  173.     if(stat(file,&stat_buff)<0)
  174.     {    perror(app_name);
  175.     return(FALSE);
  176.     };
  177.     if(stat_buff.st_size>BUFFER_SIZE)
  178.     {    fprintf(stderr,"%s: %s too long for buffer\n",app_name,file);
  179.     close(fd);
  180.     return(FALSE);
  181.     };
  182.     len=stat_buff.st_size;
  183.     if (len > buf_size) {
  184.         free (file_buff);
  185.     file_buff = lalloc(len);
  186.     buf_size = len;
  187.     }
  188.  
  189.     /* ...and fill the buffer */
  190.     if(lread(fd,(BYTE*)file_buff,len)!=len)
  191.     {    fprintf(stderr,"%s: problem reading %s\n",app_name,file);
  192.     close(fd);
  193.     free((char*)file_buff);
  194.     return(FALSE);
  195.     };
  196.     close(fd);
  197.  
  198.     /* send it to interp for the hard work */
  199.     return(interp(file_buff,file,len));
  200. };
  201.  
  202. /*
  203.  * REVISION LOG
  204.  * ============
  205.  * 0.1    SGoldthorpe    20-Mar-91    Created for Atari ST / Sozobon C.  It's
  206.  *                    a bit atari specific in places but i've
  207.  *                    tried to make it UNIX(tm) looking for
  208.  *                    easier porting (if anyone feels brave
  209.  *                    enough to try.
  210.  * 0.2    SGoldthorpe     7-Apr-91    Messed up the code in mp_intp to
  211.  *                    allow type 1 midi files.  Timing is
  212.  *                    still a bit hairy but it plays 80%
  213.  *                    of the files I have OK.
  214.  * 0.3    SGoldthorpe    27-May-91    Reformatted & tidied up, sorted out
  215.  *                    running status and added flags.
  216.  * 0.4    SGoldthorpe    20-Jul-91    Generally restructed, tidied up and
  217.  *                    added sysex capabilities.
  218.  *
  219.  */
  220.